<template>
  <div class="app-container">
    <!-- 厂商管理 -->
    <el-row>
      <el-button size="mini" type="primary" @click="dialog_newly = true" icon="el-icon-folder-add">新增</el-button>
      <!-- <el-button type="danger" icon="el-icon-delete" @click="deleteLots">删除</el-button> -->
    </el-row>
    <el-divider></el-divider>
    <!-- 表格 -->
    <el-table size="medium" :data="tableData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange" border>
      <!-- <el-table-column type="selection" width="50" align="center"></el-table-column> -->
      <el-table-column prop="name" label="厂商名称" align="center"></el-table-column>
      <el-table-column prop="handle" label="操作" align="center">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="primary" plain @click="updateCampus(scope.row)">修改</el-button> -->
          <el-button size="mini" type="danger" plain @click="deleteManufacturer({ id: scope.row.id })">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <el-pagination class="pagination" background layout="prev, pager, next" :total="total" :page-size="10"
      @current-change="handleCurrentChange"></el-pagination> -->

    <!-- 添加弹窗 -->
    <el-dialog title="添加厂商信息" :visible.sync="dialog_newly">
      <el-form :label-position="labelPosition" label-width="100px" style="margin-left:20%;">
        <el-form-item label="厂商名 :">
          <el-input v-model="newManufacturer" style="width:60%" placeholder="请输入厂商名" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_newly = false">取 消</el-button>
        <el-button type="primary" @click="addNewManufacturer" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改弹窗 -->
    <!-- <el-dialog title="修改厂商信息" :visible.sync="dialog_change">
      <el-form :model="formChangeCampus" :label-position="labelPosition" label-width="100px" style="margin-left:20%;">
        <el-form-item label="厂商名 :">
          <el-input v-model="formChangeCampus.name" style="width:60%" placeholder="请输入厂商名" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_change = false">取 消</el-button>
        <el-button type="primary" @click="updateCampusConfirm" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog> -->

  </div>
</template>

<script>
import { getManufacturer, addManufacturer, deleteManufacturer } from '@/api/ManufacturerManage/index.js'
export default {
  data() {
    return {
      labelPosition: "left",
      dialog_change: false,
      dialog_newly: false,
      tableData: [],
      multipleSelection: [],
      form: {
        current: 0,
        field: "",
        order: "",
        pageSize: 10
      },
      newManufacturer: "",
      // formNewCampus: {
      //   address: '',
      //   name: '',
      //   state: ''
      // },
      // formChangeCampus: {
      //   address: '',
      //   name: '',
      //   state: ''
      // },
      total: 0
    }
  },
  methods: {
    handleSelectionChange(val) {
      var multipleSelection = []
      val.forEach(item => {
        multipleSelection.push(item.id)
      })
      this.multipleSelection = multipleSelection
      var test = []
      this.multipleSelection.forEach(item => {
        var one = { id: item }
        test.push(one)
      })
      let obj = { ...test }
      console.log(obj)
      this.multipleSelection = obj
      // console.log(this.multipleSelection)
    },
    deleteLots() {
      this.deleteCampus(this.multipleSelection)
    },

    //删除厂商
    deleteManufacturer(e) {
      this.$confirm('此操作将删除该厂商, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteManufacturer(e).then(res => {
          if (res.data.success == true) {
            this.$message({
              message: '删除成功!',
              duration: 1000,
              type: 'success',
              onClose: () => {
                this.getManufacturer()
              }
            })
          } else {
            this.$message({
              message: res.data.message,
              duration: 2000,
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // // 修改厂商弹窗
    // updateCampus(e) {
    //   this.dialog_change = true
    //   this.formChangeCampus = JSON.parse(JSON.stringify(e))
    // },
    // // 修改厂商确认
    // updateCampusConfirm() {
    //   updateCampus(this.formChangeCampus).then(res => {
    //     if (res.status == 200) {
    //       this.dialog_change = false
    //       this.$message({
    //         message: '修改成功!',
    //         duration: 1000,
    //         type: 'success',
    //         onClose: () => {
    //           this.getCampusList()
    //         }
    //       })
    //     }
    //   })

    // },
    // 添加厂商
    addNewManufacturer() {
      addManufacturer({ name: this.newManufacturer }).then(res => {
        if (res.data.success == true) {
          this.dialog_newly = false
          this.$message({
            message: '添加成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.getManufacturer()
            }
          })
        } else {
          this.$message({
            message: res.data.message,
            duration: 1000
          })
        }
      })
    },

    handleCurrentChange(e) {
      //当前页
      this.form.current = e
      this.getCampusList()
    },
    // 查询厂商
    getManufacturer() {
      getManufacturer(

      ).then(res => {
        if (res.data.success == true) {
          this.tableData = res.data.data
          // this.total = res.data.data.total
        } else {
          this.tableData = []
          this.$message({
            message: res.data.message,
            duration: 1000
          })
        }
      })
    }
  },
  created() {
    this.getManufacturer()
  },
};
</script>
<style lang="scss" scoped>
.pagination {
  float: right;
  margin-top: 10px;
}
</style>
